package com.bestom.communication.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bestom.communication.R;
import com.bestom.communication.sql.DbManage;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SwipeAdapter extends RecyclerView.Adapter<SwipeAdapter.ViewHolder> {

    private final String TAG = "SwipeAdapter";
    private int mOpenPosition = -1;

    private final List<String> mStrings;
    private final Context mContext;
    private final EditText mEditText;
    private final String mTableName;
    private SwipeLayout mOpenSwipeLayout;


    public SwipeAdapter(Context context, String tableName, List<String> strings, EditText editText) {
        this.mContext = context;
        this.mTableName = tableName;
        this.mStrings = strings;
        this.mEditText = editText;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.swipelayout, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.delete.setOnClickListener(v -> {
            int position = viewHolder.getAdapterPosition();
            //删除内容
            DbManage.getInstance(mContext).deleteData(mTableName,mStrings.get(position));
            mStrings.remove(position);
            notifyItemRemoved(position);
            //关闭当前滑动组件
            mOpenPosition = -1;
            viewHolder.swipeLayout.smoothClose();
        });
        viewHolder.edit.setOnClickListener(v -> {
            //若当前mEditText存在父类，先让父类移除mEditText，才可以再设置到AlertDialog上
            ViewGroup parent1 = (ViewGroup) mEditText.getParent();
            if (parent1 != null) parent1.removeView(mEditText);
            int position = viewHolder.getAdapterPosition();
            String oldString = mStrings.get(position);
            mEditText.setText(oldString);
            new AlertDialog.Builder(mContext)
                    .setTitle("修改主题")
                    .setView(mEditText)
                    .setPositiveButton("修改", (dialog, which) -> {
                        String newString = mEditText.getText().toString();
                        if (!oldString.equals(newString)){
                            //更新列表及数据库
                            mStrings.remove(position);
                            mStrings.add(position, newString);
                            notifyItemChanged(position);
                            DbManage.getInstance(mContext).update(mTableName, oldString, newString);
                            //关闭当前滑动组件
                            mOpenPosition = -1;
                            viewHolder.swipeLayout.smoothClose();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
        viewHolder.swipeLayout.setSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onOpenListener(SwipeLayout swipeLayout) {
                if (mOpenSwipeLayout != null && mOpenSwipeLayout != swipeLayout) {
                    mOpenSwipeLayout.smoothClose();
                }
                mOpenSwipeLayout = swipeLayout;
                mOpenPosition = viewHolder.getAdapterPosition();
                Log.d(TAG, "mOpenPosition onOpenListener" + mOpenPosition);
            }

            @Override
            public void onCloseListener(SwipeLayout swipeLayout) {
                mOpenPosition = -1;
                Log.d(TAG, "mOpenPosition onCloseListener" + mOpenPosition);
            }

            @Override
            public void onClickListener() {
//                Toast.makeText(context, "item click" + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SwipeLayout swipeLayout = holder.swipeLayout;
        if (mOpenPosition == position && swipeLayout.getState() == SwipeLayout.CLOSE) {
            swipeLayout.setState(SwipeLayout.OPEN);
            mOpenSwipeLayout = swipeLayout;
        } else if (swipeLayout.getState() == SwipeLayout.OPEN) {
            swipeLayout.setState(SwipeLayout.CLOSE);
        }
        String str = mStrings.get(position);
        holder.text.setText(str);
    }

    @Override
    public int getItemCount() {
        return mStrings == null ? 0 : mStrings.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.delete)
        View delete;
        @BindView(R.id.edit)
        View edit;
        @BindView(R.id.text)
        TextView text;
        @BindView(R.id.swipe_layout)
        SwipeLayout swipeLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
